/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.hisense.settings;

import com.android.settings.R;
import static com.google.android.tv.settings.TvSettings.System.SCREEN_DIM_TIMEOUT;
import static android.provider.Settings.System.SCREEN_OFF_TIMEOUT;
import static android.provider.Settings.System.SOUND_EFFECTS_ENABLED;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.media.AudioManager;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.provider.Settings;
import android.preference.PreferenceScreen;

import java.util.List;
import android.util.Log;

import com.hisense.settings.util.Wrapper;
//import com.marvell.willowtv.framework.features.TvFramework;
//import com.marvell.willowtv.framework.util.atsc.ClosedCaptionEnumClass.CCAnalogDigitalServiceType;

/**
 * Handles settings menu for cc settings.  
 *
 * @author huangjiufa
 */
public class CCSettings extends PreferenceActivity implements
        Preference.OnPreferenceChangeListener{
   
    private static final String TAG = "CCSettings";

    private static final String KEY_DISPLAY_OPTION = "cc_display_option";
    private static final String KEY_CC_DISPLAY = "cc_display";
    private static final String KEY_ANALOG_CC = "analog_cc";
    private static final String KEY_DIGIAL_CC = "digital_cc";
	
  	//Off 0
  	//On 1
  	//On when muting 2
	private static final int VALUE_DEFAULT_CC_DISPLAY = 0;
	
  	//Off 0
	//CC1  1
	//CC2  2
	//CC3  3
	//CC4  4
	//Text1  5
	//Text2  6
	//Text3  7
	//Text4  8
	private static final int VALUE_DEFAULT_ANALOG_CC = 0;
	
  	//Off 0
	//Service1 1
	//Service2 2
	//Service3 3
	//Service4 4
	//Service5 5
	//Service6 6
	private static final int VALUE_DEFAULT_DIGIAL_CC = 0;
	private int CCvalue=0;

	
    private ListPreference mCCDisplayPreference;
    private ListPreference mAnalogCCPreference;
    private ListPreference mDigitalCCPreference;
	private PreferenceScreen mDigitalCCOptionPreference;
        private Context mContext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        addPreferencesFromResource(R.xml.cc_settings);
		mContext=getApplicationContext();
		
		mCCDisplayPreference = (ListPreference) findPreference(KEY_CC_DISPLAY);
		final int currentCCDisplay = getCCDisplay(VALUE_DEFAULT_CC_DISPLAY);
		Log.d(TAG, "&&&&&&&&&&&&&  currentCCDisplay value: " + currentCCDisplay);
		mCCDisplayPreference.setValue((String.valueOf(currentCCDisplay)));
		updateCCDisplaySummary(String.valueOf(currentCCDisplay));
		mCCDisplayPreference.setOnPreferenceChangeListener(this);

		mAnalogCCPreference = (ListPreference) findPreference(KEY_ANALOG_CC);
		 int currentAnalogCC= getAnalogCC(VALUE_DEFAULT_ANALOG_CC);
	
		Log.d(TAG, "&&&&&&&&&&&&&  currentAnalogCC value: " + currentAnalogCC);
		mAnalogCCPreference.setValue((String.valueOf(currentAnalogCC)));
		updateAnalogCCSummary((String.valueOf(currentAnalogCC)));
		mAnalogCCPreference.setOnPreferenceChangeListener(this);

		mDigitalCCPreference = (ListPreference) findPreference(KEY_DIGIAL_CC);
		 int currentDigitalCC= getDigitalCC(VALUE_DEFAULT_DIGIAL_CC);
		
		Log.d(TAG, "&&&&&&&&&&&&&  currentDigitalCC value: " + currentDigitalCC);
		mDigitalCCPreference.setValue((String.valueOf(currentDigitalCC)));
		updateDigitalCCSummary((String.valueOf(currentDigitalCC)));
		mDigitalCCPreference.setOnPreferenceChangeListener(this);

		mDigitalCCOptionPreference = (PreferenceScreen) findPreference(KEY_DISPLAY_OPTION);

		if(currentCCDisplay == 0)//off
		{
			mAnalogCCPreference.setEnabled(false);
			mDigitalCCPreference.setEnabled(false);
			mDigitalCCOptionPreference.setEnabled(false);
		}
	}
	
	
	
	private int getCCDisplay(int defaultValue) {
		int value = defaultValue;
		// TODO huangjiufa 
		
		value = Wrapper.getWrapperInstance().getClosedCaptions();
		Log.d(TAG, "&&&&&&&&&&&&&  getCCDisplay value: " + value);
		return value;
	}
	
	
	private void setCCDisplay(int defaultValue) {
		int value = defaultValue;
		Log.d(TAG, "&&&&&&&&&&&&&  setCCDisplay value: " + value);
		Wrapper.getWrapperInstance().setClosedCaptions(value);
		//mute 
		if(defaultValue==2)
		{
		        
			Wrapper.getWrapperInstance().setClosedCaptionsForMute(defaultValue,mContext);
		}

		/*cc choice on ,then analogCC and digitalCC must be set on*/
		if(defaultValue==1)
		{
			setDigitalCC(1);
		        updateAnalogCCSummary(String.valueOf(1));
			setAnalogCC(1);
			updateDigitalCCSummary(String.valueOf(1));
		}
	
	}

	
	private int getAnalogCC(int defaultValue) {
		int value = defaultValue;
		// TODO huangjiufa 
		
	
		value = Wrapper.getWrapperInstance().getAnalogCC();
		Log.d(TAG, "&&&&&&&&&&&&&  getAnalogCC value: " + value);
		return value;
	}
	
	
	private void setAnalogCC(int defaultValue) {
		int value = defaultValue;
		// TODO huangjiufa 
		
		Log.d(TAG, "&&&&&&&&&&&&&  setAnalogCC value: " + value);
		Wrapper.getWrapperInstance().setAnalogCC(value);
	}
	

	private int getDigitalCC(int defaultValue) {
		int value = defaultValue;
		int cc_value = Wrapper.getWrapperInstance().getDigitalCC();
		Log.d(TAG, "&&&&&&&&&&&&&  getDigitalCC  cc_value: " + cc_value);
		//value = (cc_value == CCAnalogDigitalServiceType.OFF.ordinal())?cc_value:(cc_value - CCAnalogDigitalServiceType.CS1.ordinal()+1);
		value = 0;
		Log.d(TAG, "&&&&&&&&&&&&&  getDigitalCC value: " + value);
		return value;
	}
	
	
	private void setDigitalCC(int defaultValue) {
		int value = defaultValue;
		/*if (value != 0)
		{
			value = value + CCAnalogDigitalServiceType.CS1.ordinal() - 1 ;
		}*/
		
		Log.d(TAG, "&&&&&&&&&&&&&  setDigitalCC value: " + value);
		Wrapper.getWrapperInstance().setDigitalCC(value);
	
	}
	
	
	
	private void updateCCDisplaySummary(Object value) {
		CharSequence[] summaries = getResources().getTextArray(R.array.cc_display_entries);
		CharSequence[] values = mCCDisplayPreference.getEntryValues();
		for (int i=0; i<values.length; i++) {
			//Log.i("foo", "Comparing entry "+ values[i] + " to current "
			//		  + mAnimations.getValue());
			if (values[i].equals(value)) {
				mCCDisplayPreference.setSummary(summaries[i]);
				break;
			}
		}
	}

	
	private void updateAnalogCCSummary(Object value) {
		CharSequence[] summaries = getResources().getTextArray(R.array.analog_cc_entries);
		CharSequence[] values = mAnalogCCPreference.getEntryValues();
		for (int i=0; i<values.length; i++) {
			//Log.i("foo", "Comparing entry "+ values[i] + " to current "
			//		  + mAnimations.getValue());
			if (values[i].equals(value)) {
				mAnalogCCPreference.setSummary(summaries[i]);
				break;
			}
		}
	}
	

	private void updateDigitalCCSummary(Object value) {
		CharSequence[] summaries = getResources().getTextArray(R.array.digital_cc_entries);
		CharSequence[] values = mDigitalCCPreference.getEntryValues();
		for (int i=0; i<values.length; i++) {
			//Log.i("foo", "Comparing entry "+ values[i] + " to current "
			//		  + mAnimations.getValue());
			if (values[i].equals(value)) {
				mDigitalCCPreference.setSummary(summaries[i]);
				break;
			}
		}
	}

	private void updateCClist(){
		mContext=getApplicationContext();

		mAnalogCCPreference = (ListPreference) findPreference(KEY_ANALOG_CC);
		 int currentAnalogCC= getAnalogCC(CCvalue);
	
		Log.d(TAG, "&&&&&&&&&&&&&  currentAnalogCC value: " + currentAnalogCC);
		mAnalogCCPreference.setValue((String.valueOf(currentAnalogCC)));
		updateAnalogCCSummary((String.valueOf(currentAnalogCC)));
		mAnalogCCPreference.setOnPreferenceChangeListener(this);

		mDigitalCCPreference = (ListPreference) findPreference(KEY_DIGIAL_CC);
		 int currentDigitalCC= getDigitalCC(CCvalue);
		
		Log.d(TAG, "&&&&&&&&&&&&&  currentDigitalCC value: " + currentDigitalCC);
		mDigitalCCPreference.setValue((String.valueOf(currentDigitalCC)));
		updateDigitalCCSummary((String.valueOf(currentDigitalCC)));
		mDigitalCCPreference.setOnPreferenceChangeListener(this);

		mDigitalCCOptionPreference = (PreferenceScreen) findPreference(KEY_DISPLAY_OPTION);		
		}
	
	
	@Override
	public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
		return false;
	}
	
	public boolean onPreferenceChange(Preference preference, Object objValue) {
		final String key = preference.getKey();
		if (KEY_CC_DISPLAY.equals(key)) {
			int value = Integer.parseInt((String) objValue);
			try {
				if(value != 0)//!=0
				{
					mAnalogCCPreference.setEnabled(true);
					mDigitalCCPreference.setEnabled(true);
					mDigitalCCOptionPreference.setEnabled(true);
				}
				else
				{
					mAnalogCCPreference.setEnabled(false);
					mDigitalCCPreference.setEnabled(false);
					mDigitalCCOptionPreference.setEnabled(false);
				}
				
				setCCDisplay(value);
				updateCCDisplaySummary(objValue);
				updateCClist();
			} catch (NumberFormatException e) {
				Log.e(TAG, "could not persist screen timeout setting", e);
			}
		}
		else if (KEY_ANALOG_CC.equals(key)) {
			int value = Integer.parseInt((String) objValue);
			CCvalue=value;
			try {
				setAnalogCC(value);
				updateAnalogCCSummary(objValue);
			} catch (NumberFormatException e) {
				Log.e(TAG, "could not persist screen timeout setting", e);
			}
		}
		else if (KEY_DIGIAL_CC.equals(key)) {
			int value = Integer.parseInt((String) objValue);
			try {
				setDigitalCC(value);
				updateDigitalCCSummary(objValue);
			} catch (NumberFormatException e) {
				Log.e(TAG, "could not persist screen timeout setting", e);
			}
		}
	
		return true;
	}

}
